/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}

/**
 * 判断是否是外部链接
 *
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}

/**
 * @function 时间戳转换为特定格式的日期字符串
 */
export const formatTimestamp = (timestamp: number | string) => {
  const date = new Date(timestamp); // 根据时间戳创建 Date 对象
  const year = date.getFullYear(); // 获取年份
  const month = (date.getMonth() + 1).toString().padStart(2, "0"); // 获取月份，补0
  const day = date.getDate().toString().padStart(2, "0"); // 获取日期，补0
  const hours = date.getHours().toString().padStart(2, "0"); // 获取小时，补0
  const minutes = date.getMinutes().toString().padStart(2, "0"); // 获取分钟，补0
  const seconds = date.getSeconds().toString().padStart(2, "0"); // 获取秒钟，补0
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; // 返回格式化后的日期字符串
};
